package com.mocha.nezha.controller;

import cn.hutool.core.collection.CollectionUtil;
import cn.hutool.core.util.StrUtil;
import com.mocha.common.core.MochaResponse;
import com.mocha.common.core.ResponseCode;
import com.mocha.nezha.dmo.VehicleTrackDTO;
import com.mocha.nezha.nezha.VehicleTrackHandler;
import com.scshyzh.concurrent.MochaExecutorManager;
import lombok.AllArgsConstructor;
import lombok.extern.slf4j.Slf4j;
import org.springframework.web.bind.annotation.*;

import java.util.List;
import java.util.concurrent.Executor;

/**
 * 数据从算法组
 * 算法组 有数据 就会调用该接口
 * @author songkui
 * @since 2024/3/15 23:15
 */
@RestController
@RequestMapping("/source")
@Slf4j
@AllArgsConstructor
public class DataSourceController {

    private final VehicleTrackHandler vehicleTrackHandler;

    //header : park : 园区编码;
    @PostMapping("/vehicleTrack")
    public MochaResponse<String> vehicleTrack(@RequestHeader("park") String park, @RequestBody List<VehicleTrackDTO> data){

        if (StrUtil.isEmpty(park) || CollectionUtil.isEmpty(data)){
            log.error("handler>> park or data id  is null");
            return MochaResponse.responseCode(ResponseCode.ParamInvalid);
        }

        for (VehicleTrackDTO dto : data){
            if (StrUtil.isEmpty(dto.getCno()) || StrUtil.isEmpty(dto.getVno())){
                log.warn("handler>>  cameraNo or vehicleNo is null");
            }else {
                Executor executor = MochaExecutorManager.getInstance().getDisruptor(dto.getOid().hashCode());
                executor = null == executor ? MochaExecutorManager.getInstance().getDefaultExecutor() : executor;
                executor.execute(() ->vehicleTrackHandler.doTrack(park, dto));
            }
        }

        return MochaResponse.responseCode(ResponseCode.Success);
    }


}
